//
//  HSAwardPushVC.m
//  HolyShit
//
//  Created by Robin on 2017/4/15.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSAwardPushVC.h"

static NSString *const HSAwardPushConfigKey = @"HSAwardPushConfigKey";

@interface HSAwardPushVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UILabel *tipLb;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<NSString *> *datas;

@end

@implementation HSAwardPushVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"开奖推送设置";
    [self.view addSubview:self.tableView];
    self.datas = [[NSUserDefaults standardUserDefaults] objectForKey:HSAwardPushConfigKey];
    if (!self.datas) {
        self.datas = @[@"0",@"0",@"0",@"0",@"0",@"0",@"0"].mutableCopy;
    }
    [self.tableView reloadData];
}

- (void)viewDidDisappear:(BOOL)animated
{
    NSArray<UITableViewCell *> *cells = [self.tableView visibleCells];
    __block NSMutableArray<NSString *> *datas = @[].mutableCopy;
    [cells enumerateObjectsUsingBlock:^(UITableViewCell * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UISwitch *sw = [obj viewWithTag:2333];
        [datas addObject:sw.isOn ? @"1" : @"0"];
    }];
    [[NSUserDefaults standardUserDefaults] setObject:datas forKey:HSAwardPushConfigKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [super viewDidDisappear:animated];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid = @"cellid";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellid];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(ScreenWidth - 65, 5, 80, 34)];
        sw.onTintColor = HEXCOLOR(appNavColor);
        sw.tag = 2333;
        [cell addSubview:sw];
        [cell.detailTextLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(cell.textLabel.mas_right).offset(10);
            make.right.mas_equalTo(-75);
            make.centerY.equalTo(cell);
        }];
    }
    UISwitch *sw = [cell viewWithTag:2333];
    sw.on = self.datas[indexPath.row].integerValue == 1;
    switch (indexPath.row) {
        case 0:{
            cell.textLabel.text = @"福彩3D";
            cell.detailTextLabel.text = @"每天开奖";
        }
            break;
        case 1:{
            cell.textLabel.text = @"双色球";
            cell.detailTextLabel.text = @"每周二、四、日开奖";
        }
            break;
        case 2:{
            cell.textLabel.text = @"大乐透";
            cell.detailTextLabel.text = @"每周一、三、六开奖";
        }
            break;
        case 3:{
            cell.textLabel.text = @"七乐彩";
            cell.detailTextLabel.text = @"每周一、三、五开奖";
        }
            break;
        case 4:{
            cell.textLabel.text = @"七星彩";
            cell.detailTextLabel.text = @"每周二、五、日开奖";
        }
            break;
        case 5:{
            cell.textLabel.text = @"排列3";
            cell.detailTextLabel.text = @"每天开奖";
        }
            break;
        case 6:{
            cell.textLabel.text = @"排列5";
            cell.detailTextLabel.text = @"每天开奖";
        }
            break;
        default:
            break;
    }
    if (indexPath.row < 6) {
        [cell showLineForDirection:MYDirectionBottom leftPad:15];
    }
    else{
        [cell hideLineForDirection:MYDirectionBottom];
    }
    [cell layoutSubviews];
    return cell;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = HEXCOLOR(HSColorBgLight);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
        
        self.headerView = [[UIView alloc] initWithFrame:CGRectMake(30,-20,ScreenWidth - 60, 0)];
        self.headerView.backgroundColor = HEXCOLOR(0xfefefc);
        self.headerView.layer.cornerRadius = 10;
        self.headerView.layer.borderColor = HEXCOLOR(HSColorSeparateLight).CGColor;
        self.headerView.layer.borderWidth = 0.5;
        
        self.tipLb = [UILabel labelWithFrame:CGRectMake(10, 30, self.headerView.width - 90 , 0) font:MYFont(Font28) color:HEXCOLOR(0x8b8376) text:@"打开开奖设置后即可在开奖后立即收到推送，获知开奖号码"];
//        self.tipLb.textAlignment = NSTextAlignmentCenter;
        self.tipLb.numberOfLines = 0;
        [self.tipLb sizeToFit];
        self.tipLb.width = self.headerView.width - 20;
        [self.headerView addSubview:self.tipLb];
        self.headerView.height = self.tipLb.bottom + 10;
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, self.headerView.height - 10)];
        [view addSubview:self.headerView];
        _tableView.tableHeaderView = view;
    }
    return _tableView;
}


@end
